<template>
  <el-dialog
    :close-on-click-modal="false"
    :title="isEdit?'修改岗位':'新增岗位'"
    width="800px"
    :visible.sync="visible"
  >
    <el-form :model="form" ref="form" :rules="rules" label-width="80px">
      <el-form-item v-show="false" label="id">
        <el-input autocomplete="false" v-model="form.positionId" />
      </el-form-item>
      <el-form-item label="岗位名称" prop="positionName">
        <el-input
          type="text"
          placeholder="请输入岗位名称"
          autocomplete="false"
          v-model="form.positionName"
        />
      </el-form-item>
      <el-form-item label="排序" prop="positionOrder">
        <el-input-number
          :max="100"
          placeholder="请输入数字"
          autocomplete="false"
          v-model="form.positionOrder"
        />
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="handleOk">{{ isEdit ? '修改' : '新增' }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addPosition, updatePosition } from '@/api/position'
import { openNotification } from '@/utils/util'
import pick from 'lodash.pick'

export default {
  data() {
    return {
      isEdit: true,
      visible: false,
      confirmLoading: false,
      form: {
        positionId: null,
        positionName: null,
        positionOrder: 0
      },
      rules: {
        positionName: [{ required: true, message: '请输入岗位名称', trigger: 'blur' }]
        // positionOrder: [
        //   { required: true, message: '请输入排序值', trigger: 'blur' },
        //   { type: 'number', message: '排序值为数字' }
        // ]
      }
    }
  },
  methods: {
    eidt(bool, param) {
      console.log('bool', bool, param)
      this.visible = true
      this.form = Object.assign({}, param)
      this.isEdit = bool
      if (!bool) {
        this.$set(this.form, 'positionOrder', 0)
      }
    },
    handleOk(e) {
      e.stopPropagation()
      this.$refs.form.validate(async valid => {
        if (valid) {
          if (this.isEdit) {
            const res = await updatePosition(this.form)
          } else {
            const res = await addPosition({ ...this.form, flag: false })
          }
          const mes = this.isEdit ? '修改操作成功' : '新增操作成功'
          openNotification('success', mes)
          this.visible = false
          this.$emit('refresh')
        }
      })
    }
  }
}
</script>
